import { Component, OnInit, ViewChild, ElementRef } from '@angular/core';
import { PersonInfoService } from 'src/app/services/project/smart-site/person-info';
import { Router } from '@angular/router';
import { ManagerModal, RosterModal } from './person-info.model';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-person-info',
  templateUrl: './person-info.component.html',
  styleUrls: ['./person-info.component.scss']
})
export class PersonInfoComponent implements OnInit {
  public projectInfo: any = JSON.parse(window.localStorage.getItem('project'));
  public managerPageInfo = {
    nzPageIndex: 1,
    maxResultCount: 27,
    nzTotal: 0
  };
  public rosterPageInfo = {
    nzPageIndex: 1,
    maxResultCount: 10,
    nzTotal: 0
  };
  // 管理人员表单信息
  public managerList = [];
  public managerModal = new ManagerModal();
  public isManageVisible = false;
  // 当前编辑的管理人员

  public currentManager = {
    name: null,
    gender: null
  };
  // 人员花名册
  public rosterList = [];
  public rosterModal = new RosterModal();
  public isPeopleVisible = false;
  // 编辑人员下拉列表选项
  public optionList = {
    peopleTypeList: [
      { label: '管理人员', value: 1 }, { label: '作业人员', value: 2 }
    ],
    // 当前编辑的人员
    currenPeopleType: {},
    currentTeam: null,
    currentMajor: null,
    currentName: null,
    currentGender: null,
    teamList: [],
    majorList: []
  };


  public tableHeight = {
    y: (window.innerHeight * 0.65) + 'px'
  };
  public searchfilter = '';


  constructor(
    private router: Router,
    private personInfoService: PersonInfoService,
    public message: NzMessageService,
  ) { }








  // 获取管理员信息列表
  async handleQueryManagerList(params) {
    const Managerparams = {};
    Object.assign(Managerparams, params, {
      code: this.projectInfo.code,
      maxResultCount: this.managerPageInfo.maxResultCount,
      type: 1
    });

    const res = await this.personInfoService.QueryProjectUsersPagedList(Managerparams);
    this.managerList = res.result.items;
    this.managerPageInfo.nzTotal = res.result.totalCount;
  }
  // 编辑管理员信息
  async editManagerList(item) {
    console.log(item);
    this.isManageVisible = true;
    this.currentManager.name = item.name;
    this.currentManager.gender = item.gender;
    this.managerModal = new ManagerModal(item);

  }

  // 确认编辑管理人员
  async handleManageOk() {
    console.log(this.managerModal);
    const res = await this.personInfoService.ModifyProjectUsers(this.managerModal);
    if (res.success) {
      this.isManageVisible = false;
      this.message.success('操作成功');
      // 清空数据
      this.managerModal = new ManagerModal();
      this.currentManager.name = null;
      this.currentManager.gender = null;
      // 重新获取列表
      this.handleQueryManagerList({
        skipCount: 0,
      });

      this.handleQueryRosterList({
        skipCount: 0,
      });
    }
  }
  // 关闭管理人员编辑界面
  public handleManageCancel() {
    this.isManageVisible = false;
    this.managerModal = new ManagerModal();
  }

  // 获取人员花名册列表 及下拉框选项
  async handleQueryRosterList(params) {
    const Rosterparams = {};
    Object.assign(Rosterparams, params, {
      code: this.projectInfo.code,
      maxResultCount: this.rosterPageInfo.maxResultCount,
    });

    // 人员花名册列表
    const res = await this.personInfoService.QueryProjectUsersPagedList(Rosterparams);
    if (!res.success) { this.message.warning('数据获取失败'); return; }
    this.rosterList = res.result.items;
    this.rosterPageInfo.nzTotal = res.result.totalCount;




    // 班组选项
    this.personInfoService.QueryTeamsPagedList({ maxResultCount: 2000 }).then((res) => {
      if (res.success) {
        // console.log(res.result.items)
        const Temp = res.result.items;
        const tempOrg = [];
        Temp.forEach(element => {
          tempOrg.push({ label: element.name, value: element.id });
        });
        this.optionList.teamList = tempOrg;
      } else {
        this.message.warning(res.error.message);
      }
    });



    // 获取工种选项
    this.personInfoService.QueryMajorsPagedList({ maxResultCount: 2000 }).then((res) => {
      if (res.success) {
        // console.log(res.result.items)
        const Temp = res.result.items;
        const tempOrg = [];
        Temp.forEach(element => {
          tempOrg.push({ label: element.name, value: element.id });
        });
        this.optionList.majorList = tempOrg;
      } else {
        this.message.warning(res.error.message);
      }
    });
  }

  // 编辑人员花名册
  async editrosterList(item) {
    console.log(item);
    this.isPeopleVisible = true;
    // 初始化清空变量
    this.rosterModal = new RosterModal(item);
    this.optionList.currentTeam = null;
    this.optionList.currentMajor = null;
    this.optionList.currentName = item.name;
    this.optionList.currentGender = item.gender;

    // 绑定值
    if (item.type === 1) {
      this.optionList.currenPeopleType = { label: '管理人员', value: 1 }; // 绑定当前人员类别
    } else if (item.type === 2) {
      this.optionList.currenPeopleType = { label: '作业人员', value: 2 };
    }
    if (item.teamName && item.teamId) {
      this.optionList.currentTeam = { label: item.teamName, value: item.teamId }; // 绑定当前班组
    }
    if (item.majorName && item.majorId) {
      this.optionList.currentMajor = { label: item.majorName, value: item.majorId }; // 绑定当前工种
    }


  }

  // 确认编辑人员花名册
  public async handlePeopleOk() {

    this.rosterModal.type = this.optionList.currenPeopleType['value'];
    this.rosterModal.majorId = this.optionList.currentMajor['value'];
    this.rosterModal.teamId = this.optionList.currentTeam['value'];
    console.log(this.rosterModal);

    const res = await this.personInfoService.ModifyProjectUsers(this.rosterModal);
    if (res.success) {
      this.isPeopleVisible = false;
      this.message.success('操作成功');
      this.optionList.currentTeam = null;
      this.optionList.currentMajor = null;
      this.handleQueryManagerList({
        skipCount: 0,
      });

      this.handleQueryRosterList({
        skipCount: 0,
      });
    }

  }
  // 选择框改变事件
  selChange(value: { label: string; value: any; }): void {
    // console.log(value);
  }
  // 绑定 输入框
  compareFn = (o1: any, o2: any) => (o1 && o2 ? o1.value === o2.value : o1 === o2);

  // 关闭人员花名册编辑界面
  public handlePeopleCancel() {
    this.isPeopleVisible = false;
    this.managerModal = new ManagerModal();
  }

  // 管理人员分页切换
  async managerPageChange(index) {
    const data = {
      skipCount: index - 1,
    };
    await this.handleQueryManagerList(data);
  }
  // 人员花名册分页切换
  async rosterPageChange(index) {
    const data = {
      skipCount: index - 1,
    };
    await this.handleQueryRosterList(data);
  }

  routeToSurvey() {
    const projectId = JSON.parse(window.localStorage.getItem('project')).id;
    this.router.navigate([`/inner/project/${projectId}/survey`], { queryParams: { info: 'project' } });
  }
  // 人员花名册最大页数
  async rosterPageSizeChange(size) {
    this.rosterPageInfo.maxResultCount = size;
    const data = {
      skipCount: 0,
    };
    await this.handleQueryRosterList(data);
  }
  // 初始化数据
  initData() {
    const data = {
      skipCount: 0,
    };
    this.handleQueryManagerList(data);
    // 实名制-管理员信息
    this.handleQueryRosterList(data);
  }

  ngOnInit() {
    this.initData();
  }

}
